package com.zkc.blog.controller.admin;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zkc.blog.pojo.Blogcate;
import com.zkc.blog.service.BlogCateService;
import com.zkc.blog.uilt.ResultEntity;

@RestController
public class BlogCateController {

	@Autowired
	private BlogCateService cateService;
	
	//增
	@PostMapping("/blogcate")
	public ResultEntity<String> addBlogCate(
			@RequestBody Blogcate cate
			){
		try {
			cateService.insertBlogCate(cate);
		} catch (Exception e) {
			// TODO: handle exception
			return ResultEntity.failed(e.getMessage());
		}
		return ResultEntity.successWithoutData();
	}
	//删
	@DeleteMapping("/blogcate")
	public ResultEntity<String> deleteBlogCate(
			@RequestBody List<Integer> cateids
			){
		try {
			cateService.DeleteBlogCate(cateids);
		} catch (Exception e) {
			// TODO: handle exception
			return ResultEntity.failed(e.getMessage());
		}
		return ResultEntity.successWithoutData();
	}
	//改
	@PutMapping("/blogcate")
	public ResultEntity<String> updateBlogCate(
			@RequestBody Blogcate cate
			){
		try {
			cateService.UpdateBlogCate(cate);
		} catch (Exception e) {
			// TODO: handle exception
			return ResultEntity.failed(e.getMessage());
		}
		return ResultEntity.successWithoutData();
	}
	//查
	@GetMapping("/blogcate")
	public ResultEntity<PageInfo<Blogcate>> getBlogCateList(
			@RequestParam(value = "searchkey", defaultValue = "") String  searchkey,
			@RequestParam(value = "start", defaultValue = "0") int start,
			@RequestParam(value = "size", defaultValue = "5") int size
			){
		try {
			PageHelper.startPage(start,size,"cateid desc");//这句要先启动
			List<Blogcate> list=cateService.getBlogCateList(searchkey);
			PageInfo<Blogcate> page = new PageInfo<>(list);//分页处理
			return ResultEntity.successWithoutData(page);
		} catch (Exception e) {
			// TODO: handle exception
			return ResultEntity.failed(e.getMessage());
		}
	}
	@GetMapping("/blogcate/{id}")
	public ResultEntity<Blogcate> getBlogCateList(
			@PathVariable Integer id
			){
		try {
			Blogcate cate=cateService.getBlogCateById(id);
			return ResultEntity.successWithoutData(cate);
		} catch (Exception e) {
			// TODO: handle exception
			return ResultEntity.failed(e.getMessage());
		}
	}
}
